var readability = require('node-readability-cheerio');
readability.read('http://etfdb.com/etf/DBJP', function (err, $) {
    if (err) return console.error(err);
    var liArr = $('.list-unstyled').eq(0).find('li');
    var topArr = $('#holdings-collapse .holdings-ol').find('li');
    var topArrL = topArr.length;
    var top_ten = [];
    var sector = [];
    for (var i = 0; i < topArrL; i++) {
        var arrr = $('#holdings-collapse .holdings-ol').find('li').eq(i).text().trim().split('\n');
        var txt = arrr[arrr.length - 1];
        top_ten.push(txt);
    }
    var chartArr = $('#holdings-collapse .chart');
    var sectorArr = [];
    if (chartArr.length > 0) {
        sectorArr = JSON.parse($('#holdings-collapse .chart').eq(1).attr('data-chart-series'));
    }
    //console.log(sectorArr);
    var sectorArrL = sectorArr.length;
    for (i = 0; i < sectorArrL; i++) {
        var sectorTxt = sectorArr[i][0].split('(');
        sector.push({
            name: sectorTxt[0].trim(),
            percentage: sectorTxt[1].substr(0, sectorTxt[1].length - 1),
            val: sectorArr[i][1]
        });
    }
    var market_cap = [];
    var market_capArr = [];
    if (chartArr.length > 0) {
        market_capArr = JSON.parse($('#holdings-collapse .chart').eq(2).attr('data-chart-series'));
    }
    var market_capArrL = market_capArr.length;
    for (i = 0; i < market_capArrL; i++) {
        var market_capTxt = market_capArr[i][0].split('(');
        market_cap.push({
            name: market_capTxt[0].trim(),
            percentage: market_capTxt[1].substr(0, market_capTxt[1].length - 1),
            val: market_capArr[i][1]
        });
    }
    var returnData = [];
    var returnArr = $('#performance-collapse .col-md-6');
    var returnArrL = returnArr.length;
    for (i = 0; i < returnArrL; i++) {
        var returnSingle = returnArr.eq(i);
        returnData.push({
            name: returnSingle.find('.relative-metric-name').text(),
            value: returnSingle.find('.relative-metric-bubble').text().trim()
        });
    }
    var obj = {
        name_en: $('.data-title').text().trim().split('\n')[1],
        issuer: liArr.eq(0).find('.pull-right').text(),
        issue_date: liArr.eq(4).find('.pull-right').text(),
        index: liArr.eq(6).find('.pull-right').text(),
        ratio: liArr.eq(2).find('.pull-right').text(),
        aum: $('.list-unstyled').eq(2).find('li').eq(0).find('.pull-right').text(),
        '1m_vol': $('.list-unstyled').eq(4).find('li').eq(0).find('.pull-right').text(),
        '2m_vol': $('.list-unstyled').eq(4).find('li').eq(1).find('.pull-right').text(),
        pe_ratio: $('#valuation-collapse .relative-metric-bubble-data').text(),
        yield: $('#dividend-collapse .relative-metric-bubble-data').text(),
        num_holding: $('#holdings-collapse .relative-metric-bubble-data').eq(0).text(),
        top_ten: top_ten,
        sector: sector,
        market_cap: market_cap,
        returnData: returnData
    };
    console.log(obj);
});